package Sudoku;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import android.util.Log;

public class CellMethod {
	
	private Random _random;
	
	public CellMethod(){
		_random = new Random();
	}
	
	// 1*9 = 9
	// 2*9 = 18
	// 3*9 = 27
	// 4*9 = 36
    public Cell[][] newTable(int mark){
    	int[][] matrix = SudokuUtil.generateShuDu();
    	Cell[][] table = new Cell[9][9];
        for (int r = 0; r < 9; r++){
            for (int c = 0; c < 9; c++){
                table[r][c] = new Cell();
                table[r][c].Value = matrix[r][c];
            }
        }
        Log.d("sv2", "begin setMart");
        for	(int br = 0; br<3; br++){
        	for(int bc = 0; bc <3; bc++){
        		setMark(table, br, bc);
        	}
        }
        
        return table;
    }
    
    public Cell[][] newTable2(int level){
    	int matrix[][]={  
                {9,7,8,3,1,2,6,4,5},  
                {3,1,2,6,4,5,9,7,8},  
                {6,4,5,9,7,8,3,1,2},  
                {7,8,9,1,2,3,4,5,6},  
                {1,2,3,4,5,6,7,8,9},  
                {4,5,6,7,8,9,1,2,3},  
                {8,9,7,2,3,1,5,6,4},  
                {2,3,1,5,6,4,8,9,7},  
                {5,6,4,8,9,7,2,3,1}  
        };         
        Sudoku2.printArray(matrix);  
        ArrayList<Integer> randomList=Sudoku2.creatNineRondomArray();  
        Sudoku2.creatSudokuArray(matrix, randomList);
        
    	Cell[][] table = new Cell[9][9];
        for (int r = 0; r < 9; r++){
            for (int c = 0; c < 9; c++){
                table[r][c] = new Cell();
                table[r][c].Value = matrix[r][c];
            }
        }
        Log.d("sv2", "begin setMart");
        for	(int br = 0; br<3; br++){
        	for(int bc = 0; bc <3; bc++){
        		for(int iLevel = 0; iLevel < level; iLevel ++){
        			setMark(table, br, bc);
        		}
        	}
        }
        
        return table;
    }
    
    private void setMark(Cell[][] table, int br, int bc){
    	int sr = br * 3;
    	int sc = bc * 3;
    	int seed = this._random.nextInt(9);
    	int seedR = seed / 3;
    	int seedC = seed % 3;
    	int r = sr + seedR;
    	int c = sc + seedC;
    	
    	while(table[r][c].Value == 0){
        	seed = this._random.nextInt(9);
        	seedR = seed / 3;
        	seedC = seed % 3;
        	r = sr + seedR;
        	c = sc + seedC;
    	}
    	table[r][c].Value = 0;
    }
    
    public boolean Validate(Cell[][] table){
    	for	(int r = 0; r<9;r++){
    		for(int c=0;c<9;c++){
    			if(!validateCell(table, r, c)){
    				return false;
    			}
    		}
    	}
    	return true;
    }
    
    private boolean validateCell(Cell[][] table, int r, int c){
    	if(table[r][c].Value!=0){
    		return true;
    	}
    	
    	int cellValue = table[r][c].getValue();
    	if(cellValue == 0){
    		return false;
    	}
    	
    	for	(int cIndex = 0; cIndex < 9; cIndex++){
    		int compareValue = table[r][cIndex].getValue();
    		if(compareValue == cellValue && cIndex != c){
    			return false;
    		}
    	}
    	
    	for	(int rIndex = 0; rIndex < 9; rIndex++){
    		int compareValue = table[rIndex][c].getValue();
    		if(compareValue == cellValue && rIndex != r){
    			return false;
    		}
    	}
    	
    	int br = r / 3;
    	int bc = c / 3;
    	for	(int rIndex = 0; rIndex < 3; rIndex ++){
    		for(int cIndex = 0; cIndex < 3; cIndex++){
    			int actr = br * 3 + rIndex;
    			int actc = bc * 3 + cIndex;
        		int compareValue = table[actr][actc].getValue();
        		if(compareValue == cellValue && actr != r && actc != c){
        			return false;
        		}
    		}
    	}
    	
    	return true;
    }
}
